package com.bear.atlassianmcp.confluence.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * Confluence页面数据模型
 * 对应Confluence REST API返回的Page对象
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class ConfluencePage {

    private String id;
    private String type;
    private String status;
    private String title;
    
    @JsonProperty("_links")
    private Map<String, Object> links;
    
    @JsonProperty("_expandable")
    private Map<String, Object> expandable;
    
    private ConfluenceSpace space;
    private ConfluenceVersion version;
    private ConfluenceBody body;
    private ConfluencePageExtensions extensions;

    /**
     * 获取页面的Web URL
     */
    public String getWebUrl() {
        if (links != null && links.containsKey("webui")) {
            return (String) links.get("webui");
        }
        return null;
    }

    /**
     * 获取页面内容（Storage格式）
     */
    public String getContent() {
        if (body != null && body.getStorage() != null) {
            return body.getStorage().getValue();
        }
        return null;
    }

    /**
     * 获取页面创建时间
     */
    public String getCreatedDate() {
        if (version != null) {
            return version.getWhen();
        }
        return null;
    }

    /**
     * 获取页面创建者
     */
    public String getCreatedBy() {
        if (version != null) {
            if (version.getBy() != null) {
                return version.getBy().getDisplayName();
            } else if (version.getByUser() != null) {
                return version.getByUser().getDisplayName();
            }
        }
        return null;
    }

    /**
     * 格式化为AI友好的文本描述
     */
    public String toFormattedString() {
        StringBuilder sb = new StringBuilder();
        sb.append("📄 页面: ").append(title).append("\n");
        sb.append("🆔 ID: ").append(id).append("\n");
        sb.append("📊 状态: ").append(status).append("\n");
        sb.append("📁 空间: ").append(space != null ? space.getName() : "未知").append("\n");
        sb.append("🔢 版本: ").append(version != null ? version.getNumber() : "未知").append("\n");
        
        if (getCreatedBy() != null) {
            sb.append("👤 创建者: ").append(getCreatedBy()).append("\n");
        }
        
        if (getCreatedDate() != null) {
            sb.append("📅 创建时间: ").append(getCreatedDate()).append("\n");
        }
        
        String webUrl = getWebUrl();
        if (webUrl != null) {
            sb.append("🔗 链接: ").append(webUrl).append("\n");
        }
        
        String content = getContent();
        if (content != null && !content.trim().isEmpty()) {
            String preview = content.length() > 200 ? content.substring(0, 200) + "..." : content;
            sb.append("📝 内容预览: ").append(preview).append("\n");
        }
        
        return sb.toString();
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Map<String, Object> getLinks() {
        return links;
    }

    public void setLinks(Map<String, Object> links) {
        this.links = links;
    }

    public Map<String, Object> getExpandable() {
        return expandable;
    }

    public void setExpandable(Map<String, Object> expandable) {
        this.expandable = expandable;
    }

    public ConfluenceSpace getSpace() {
        return space;
    }

    public void setSpace(ConfluenceSpace space) {
        this.space = space;
    }

    public ConfluenceVersion getVersion() {
        return version;
    }

    public void setVersion(ConfluenceVersion version) {
        this.version = version;
    }

    public ConfluenceBody getBody() {
        return body;
    }

    public void setBody(ConfluenceBody body) {
        this.body = body;
    }

    public ConfluencePageExtensions getExtensions() {
        return extensions;
    }

    public void setExtensions(ConfluencePageExtensions extensions) {
        this.extensions = extensions;
    }
}
